#include<stdlib.h>
#include<assert.h>
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<sys/time.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<time.h>

int malarm(int which,int milliseconds)
{
	/* itimerval是精确到 */
	struct itimerval t;
	t.it_value.tv_sec = milliseconds / 1000;
	t.it_value.tv_usec = milliseconds % 1000;
	t.it_interval.tv_sec = 0;
	t.it_interval.tv_usec = 0;
	return setitimer(which, &t, NULL);
}

void fetch(int signo)
{
	static int tot = 0;
	printf("\n%d: %d\n",signo, (int) time(NULL));
	if (++tot == 3) exit(0);
}

int main()
{
	signal(SIGALRM, fetch) ;   /* 14: real time */
	signal(SIGVTALRM, fetch);  /* 26: process time */
	signal(SIGPROF, fetch);    /* 27: process time + sys call time */
	printf("%d\n",(int) time(NULL));
	malarm(ITIMER_REAL , 2000);
	malarm(ITIMER_VIRTUAL, 2000);
	malarm(ITIMER_PROF, 2000);

	char str[] = "hello";
	int fd = open("a.txt",O_WRONLY);
	int a = 1 , i;
	while (1)
	{
		for (i = 0; i < 2000000; i++) a = a + 1;
		for (i = 0; i < 1000000; i++)
		{
			//sys call time
			write(fd, str, sizeof(str));
		}
		//let process sleep
		sleep(1);
	}
	return 0;
}
